package com.audaque.springboot.foshanupload.web.raspcloud.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import com.audaque.springboot.foshanupload.authcore.model.currentUser.TransmittableThreadLocalCurrentUser;
import com.audaque.springboot.foshanupload.authcore.service.impl.LoginServiceImpl;
import com.audaque.springboot.foshanupload.web.raspcloud.entity.User;
import com.audaque.springboot.foshanupload.web.raspcloud.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * nosaveframeworklogin
 */
@Service
public class LoginExtServiceImpl extends LoginServiceImpl {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean validLoginForm(TransmittableThreadLocalCurrentUser transmittableThreadLocalCurrentUser) {
        String username = transmittableThreadLocalCurrentUser.getUsername();
        String password1 = transmittableThreadLocalCurrentUser.getPassword();
        String password = SecureUtil.md5(password1.toString());
        //根据username查询用户信息
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            return false;
        }
        if (!password.equals(user.getPassword())) {
            return false;
        }
        return true;
    }


    @Override
    public TransmittableThreadLocalCurrentUser getUserByUsername(String username) {
        //根据username查询用户信息
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        User user = userMapper.selectOne(queryWrapper);
        TransmittableThreadLocalCurrentUser transmittableThreadLocalCurrentUser = BeanUtil.toBean(user, TransmittableThreadLocalCurrentUser.class);
        return transmittableThreadLocalCurrentUser;
    }
}
